//
//  NewsViewController.m
//  家校互通
//
//  Created by 姬鑫宇 on 15/5/11.
//  Copyright (c) 2015年 姬鑫宇. All rights reserved.
//

#import "NewsViewController.h"
#import "NewsStyleOneTableViewCell.h"
#import "NewsStyleTwoTableViewCell.h"
#import "NewsMemberTableViewCell.h"

#import "ClassMemberViewController.h"
#import "CoModel.h"

#import "DSFMovieDetailFFMDBModel.h"
@interface NewsViewController ()<UITableViewDataSource,UITableViewDelegate,ConnectControlDelegate>{
    ConnectControl *connectCtr;
}

@property (nonatomic, retain) NSMutableArray *arrayList;

@end

@implementation NewsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _contentTableView.delegate=self;
    _contentTableView.dataSource=self;
    
    NSNotificationCenter *defaultCenter = [NSNotificationCenter defaultCenter];
    [defaultCenter addObserver:self selector:@selector(networkDidReceiveMessage:) name:kReciveNewsList object:nil];
    
    [defaultCenter addObserver:self selector:@selector(reloadNewsList) name:kReloadNewsList object:nil];
    
    connectCtr = [ConnectControl showForView:self.view];
    connectCtr.delegate=self;
    
    if (connectCtr.superview!=nil && connectCtr.loading==NO)
    {
        [connectCtr setLoading:YES];
    }
    // Do any additional setup after loading the view.
}

//刷新列表

- (void)reloadNewsList{
    [self connect];
}

//收到消息
- (void)networkDidReceiveMessage:(NSNotification *)notification {
    NSDictionary * userInfo = [notification userInfo];
    if (_arrayList.count) {
        TalkMessage *talk = [[TalkMessage alloc] initWithDictionary:[userInfo objectForKey:@"data"] error:nil];
        NSInteger index = 0;
        for (int x =0;x<_arrayList.count;x++){
            NewsListModel *dic = [_arrayList objectAtIndex:x];
            if (talk.chart_id == dic.id) {
                index = x;
            }
        }
        
        NewsListModel *dict = [_arrayList objectAtIndex:index];
        
        dict.msg_count = dict.msg_count+1;
        [_arrayList removeObject:dict];
        [_arrayList insertObject:dict atIndex:0];
        [_contentTableView reloadData];
    }
}

- (void)connect{
    [CoModel getClazzCharts:^(BOOL result, NSMutableArray *roomList, NSError *error) {
        if (result) {
            [ConnectControl removeFromView:self.view];
            _arrayList = [NSMutableArray arrayWithArray:roomList];
            [_contentTableView reloadData];
        }
        
    } withToken:[DSFUserInfo shareUserInstance].token andClassId:[DSFUserInfo shareUserInstance].clazz_user.clazz.id];
}

#pragma mark -UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section==0)//班级成员
    {
        NewsMemberTableViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"NewsMemberTableViewCell" forIndexPath:indexPath];
        UIView *selectView=[[UIView alloc]initWithFrame:vCell.viewForHold.frame];
        selectView.backgroundColor=COLOR(240, 240, 240);
        vCell.selectedBackgroundView=selectView;
        // vCell.selectionStyle=UITableViewCellSelectionStyleNone;
        return vCell;
    }else{
        NewsListModel *model = [_arrayList objectAtIndex:indexPath.section-1];
        
        if (model.type == 1 || model.type == 2) {//聊天室
            NewsStyleTwoTableViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"NewsStyleTwoTableViewCell" forIndexPath:indexPath];
            UIView *selectView=[[UIView alloc]initWithFrame:vCell.viewForHold.frame];
            selectView.backgroundColor=COLOR(240, 240, 240);
            vCell.selectedBackgroundView=selectView;
            [vCell setContentOfTalkRoom:model];
            
            NSInteger count = [[DSFMovieDetailFFMDBModel sharedSingleton] getCountByChartId:model.id];
            
            if (count == model.msg_count) {
                vCell.imgRedNood.hidden = YES;
            }else
                vCell.imgRedNood.hidden = NO;
            
            return vCell;
        }else if (model.type == 4 || model.type == 3) {//请假条
            NewsStyleOneTableViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"NewsStyleOneTableViewCell" forIndexPath:indexPath];
            UIView *selectView=[[UIView alloc]initWithFrame:vCell.viewForHold.frame];
            selectView.backgroundColor=COLOR(240, 240, 240);
            vCell.selectedBackgroundView=selectView;
            [vCell setContentOfJiatiao:model];
            
            NSInteger count = [[DSFMovieDetailFFMDBModel sharedSingleton] getCountByChartId:model.id];
            
            if (count == model.msg_count) {
                vCell.imgRedNood.hidden = YES;
            }else
                vCell.imgRedNood.hidden = NO;
            
            return vCell;
        }else
        {
            NewsStyleOneTableViewCell *vCell=[tableView dequeueReusableCellWithIdentifier:@"NewsStyleOneTableViewCell" forIndexPath:indexPath];
            UIView *selectView=[[UIView alloc]initWithFrame:vCell.viewForHold.frame];
            selectView.backgroundColor=COLOR(240, 240, 240);
            vCell.selectedBackgroundView=selectView;
            [vCell setContentOfJiatiao:model];
            
            NSInteger count = [[DSFMovieDetailFFMDBModel sharedSingleton] getCountByChartId:model.id];
            
            if (count == model.msg_count) {
                vCell.imgRedNood.hidden = YES;
            }else
                vCell.imgRedNood.hidden = NO;
            
            return vCell;
        }
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1+_arrayList.count;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return nil;
}
#pragma mark -UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.001;
}
// Called after the user changes the selection.
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    AppDelegate *_appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    if (indexPath.section==0) {//创建聊天室
        ClassMemberViewController *vc=[self.storyboard instantiateViewControllerWithIdentifier:@"ClassMemberViewController"];
        vc.title=@"班级成员";
        [_appDelegate.mainViewController.navigationController pushViewController:vc animated:YES];
        
    }else
    {
        NewsListModel *model = [_arrayList objectAtIndex:indexPath.section-1];
        
        if (model.type == 1 || model.type == 2) {//聊天室
            UUChatViewController *vc =[self.storyboard instantiateViewControllerWithIdentifier:@"UUChatViewController"];
            vc.title = model.title;
            vc.chartId = model.id;
            [_appDelegate.mainViewController.navigationController pushViewController:vc animated:YES];
        }else if (model.type == 4 || model.type == 3) {//请假条
            AskingForLeaveViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"AskingForLeaveViewController"];
            vc.title=@"请假条";
            vc.chartId = model.id;
            [_appDelegate.mainViewController.navigationController pushViewController:vc animated:YES];
        }

    }
    [tableView reloadRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
